package com.xiaoge.basedevlibrary.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.wuxiaolong.androidutils.library.ActivityManagerUtil;
import com.xiaoge.basedevlibrary.R;
import com.xiaoge.basedevlibrary.utils.AppUtil;
import com.xiaoge.basedevlibrary.utils.ToastUtil;

/**
 * Created by guoyzh on 2017-8-8 0008.
 */

public class GuoBaseActivity extends Activity {

    public ActivityManagerUtil activityManagerUtil;
    public Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = this;

        // 方便全局通过AppUtils获取上下文
        AppUtil.setActivity(this);

        activityManagerUtil = ActivityManagerUtil.getInstance();
        activityManagerUtil.pushOneActivity(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        AppUtil.setActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppUtil.setActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //结束Activity&从栈中移除该Activity
        activityManagerUtil.popOneActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();


        // 实现右侧退出功能必须重写这个方法  推荐将此功能写在BaseActivity中
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 再按一次退出程序
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToast(AppUtil.getContext(), "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                activityManagerUtil.appExit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
